package com.sweets.look.util;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

import java.io.File;

public final class FileUtils {


    /**
     * 删除目录文件
     * @param dir
     * @return
     */
    public static boolean deleteDir(File dir) {
        if (dir.isDirectory()) {
            String[] children = dir.list();//递归删除目录中的子目录下
            for (int i = 0; i < children.length; i++) {
                boolean success = deleteDir(new File(dir, children[i]));
                if (!success) {
                    return false;
                }
            }
        }
        // 目录此时为空，可以删除
        return dir.delete();
    }


    public static boolean fileIsExists(File f) {
        try {
            boolean bool = f.exists();
            if (!bool) {
                return false;
            }
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    /**
     * @param file
     * @return 文件后缀
     */
    public static String getFileType(File file) {
        String name = file.getName();
        return name.lastIndexOf(".") != -1 ? name.substring(name.lastIndexOf(".")) : "";
    }

    public static String getPath(Context context, Uri uri) {
        String v8;
        if ("content".equalsIgnoreCase(uri.getScheme())) {
            String[] v2 = new String[]{"_data"};
            try {
                Cursor cursor = context.getContentResolver().query(uri, v2, null, null, null);
                if (FileUtils.class.desiredAssertionStatus() && cursor == null) {
                    throw new AssertionError();
                }

                int v6 = cursor.getColumnIndexOrThrow("_data");
                if (!cursor.moveToFirst()) {
                    return null;
                }

                v8 = cursor.getString(v6);
                cursor.close();
                return v8;
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }

        if ("file".equalsIgnoreCase(uri.getScheme())) {
            v8 = uri.getPath();
        } else {
            v8 = null;
        }

        return v8;
    }
}

